package com.krmes.mes.base;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.krmes.mes.app.AppManager;
import com.krmes.mes.ui.dialog.CommonToast;
import com.krmes.mes.ui.dialog.DialogControl;
import com.krmes.mes.ui.dialog.DialogHelper;
import com.krmes.mes.ui.dialog.WaitDialog;
import com.krmes.mes.util.TDevice;

public abstract class BaseActivity
        extends Activity
        implements DialogControl, OnClickListener
{
    public static final String INTENT_ACTION_EXIT_APP = "INTENT_ACTION_EXIT_APP";
    private boolean _isVisible;
    private WaitDialog _waitDialog;
    protected LayoutInflater mInflater;

    protected int getActionBarCustomView()
    {
        return 0;
    }

    protected int getActionBarTitle()
    {
        return 2131492875;
    }

    protected int getLayoutId()
    {
        return 0;
    }

    protected void handleNotifyMessage(Message paramMessage) {}

    protected boolean hasActionBar()
    {
        return true;
    }

    protected boolean hasBackButton()
    {
        return false;
    }

    protected boolean haveSpinner()
    {
        return false;
    }

    @Override
    public void hideWaitDialog() {
        if (_isVisible && _waitDialog != null) {
            try {
                _waitDialog.dismiss();
                _waitDialog = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    protected View inflateView(int paramInt)
    {
        return this.mInflater.inflate(paramInt, null);
    }

    protected void init(Bundle paramBundle) {}

    protected void onBeforeSetContentLayout() {}

    protected void onCreate(Bundle paramBundle)
    {
        super.onCreate(paramBundle);
        AppManager.getAppManager().addActivity(this);
        onBeforeSetContentLayout();
        if (getLayoutId() != 0) {
            setContentView(getLayoutId());
        }
        this.mInflater = getLayoutInflater();
        init(paramBundle);
        this._isVisible = true;
    }

    protected void onDestroy()
    {
        super.onDestroy();
        TDevice.hideSoftKeyboard(getCurrentFocus());
    }

    public boolean onMenuOpened(int paramInt, Menu paramMenu)
    {
        return super.onMenuOpened(paramInt, paramMenu);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 16908332:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onPause()
    {
        super.onPause();
    }

    protected void onResume()
    {
        super.onResume();
    }

    public void showToast(int paramInt1, int paramInt2, int paramInt3)
    {
        showToast(getString(paramInt1), paramInt2, paramInt3);
    }

    public void showToast(String paramString, int paramInt1, int paramInt2)
    {
        CommonToast localCommonToast = new CommonToast(this);
        localCommonToast.setMessage(paramString);
        localCommonToast.setMessageIc(paramInt1);
        localCommonToast.setLayoutGravity(paramInt2);
        localCommonToast.show();
    }

    public WaitDialog showWaitDialog()
    {
        return showWaitDialog("操作进行中...");
    }

    public WaitDialog showWaitDialog(int paramInt)
    {
        return showWaitDialog(getString(paramInt));
    }
    @Override
    public WaitDialog showWaitDialog(String message) {
        if (_isVisible) {
            if (_waitDialog == null) {
                _waitDialog = DialogHelper.getWaitDialog(this, message);
            }else if (_waitDialog != null) {
                _waitDialog.setMessage(message);
                //_waitDialog.show();
            }
            _waitDialog.show();
            return _waitDialog;
        }
        return null;
    }
}
